var fakedata=require("../../static/js/fakedata");
var app=getApp();
Page({
    data: {
        userid:'',
        displayList:[
            {
                "goodId": 1,
                "marker": {
                    "addr_name": "泓仁楼",
                    "address": "陕西省西安市长安区学森一路",
                    "longitude": "108.65940226001177",
                    "latitude": "34.260012574674946"
                },
                "name": "加湿器",
                "price": "39",
                "url": "../../static/images/commodity/artifact/artifact2.jpg"
            }, {
                "goodId": 2,
                "marker": {
                    "addr_name": "泓生楼",
                    "address": "陕西省西安市长安区学森一路",
                    "longitude": "108.65954463701702",
                    "latitude": "34.25866967171034"
                },
                "name": "盆",
                "price": "49",
                "url": "../../static/images/commodity/artifact/artifact3.jpg"
            },
            {
                "goodId": 3,
                "marker": {
                    "addr_name": "兴华购物广场",
                    "address": "陕西省西安市长安区樱花西路",
                    "longitude": "108.65420105187906",
                    "latitude": "34.255402304704496"
                },
                "name": "置物架",
                "price": "10",
                "url": "../../static/images/commodity/artifact/artifact1.jpg"
            },
            {
                "goodId": 4,
                "marker": {
                    "addr_name": "朗清苑",
                    "address": "陕西省西安市长安区文治东路",
                    "longitude": "108.65588159739058",
                    "latitude": "34.258461090168375"
                },
                "name": "衣架",
                "price": "88",
                "url": "../../static/images/commodity/lifegoods/lifegoods17.jpg"
            }, {
                "goodId": 5,
                "marker": {
                    "addr_name": "泓德楼",
                    "address": "陕西省西安市长安区学森一路",
                    "longitude": "108.65859337368362",
                    "latitude": "34.25939267282698"
                },
                "name": "地毯",
                "price": "88",
                "url": "../../static/images/commodity/lifegoods/lifegoods18.jpg"
            }
        ]
    },
    onLoad(){
        if(app.globalData.islogin){
            this.setData({
                userid:app.globalData.userinfo.id
            });
            swan.request({
                url: fakedata.urls.getstars,
                header: {
                    'Content-Type': 'application/json',
                    'Developer-Server-Domain': fakedata.host,
                    'X-SP-Accept-Encoding': 'base64'
                },
                method: 'GET',
                dataType: 'json',
                responseType: 'text',
                data: {
                    userid:this.data.userid
                },
                success: res => {
                    console.log(res);
                    // this.setData({
                    //     displayList:[]
                    // });
                },
                fail: err => {
                    swan.showModal({
                        title: '错误信息',
                        content: JSON.stringify(err)
                    });
                }
            });
        }
    },
    onShow(){
        if(!app.globalData.islogin){
            swan.showModal({
                title:'提示',
                content:"您还没有登录！"
            });
            swan.navigateTo({
                url:"/pages/login/login"
            });
        }
    },
    gotoDetail(e){
        console.log(e);
        swan.navigateTo({
            url:"/pages/detail/detail?goodid="+e.currentTarget.dataset.id
        });
    },
    removeFromStar(e){
        console.log(e);
        let goodidval=e.currentTarget.dataset.id;
        swan.request({
            url: fakedata.urls.removestar,
            header: {
                'Content-Type': 'application/json',
                'Developer-Server-Domain': fakedata.host,
                'X-SP-Accept-Encoding': 'base64'
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            data: {
                userid:this.data.userid,
                goodid:goodidval
            },
            success: res => {
                console.log(res);
                updatalist(this.data.displayList,goodidval);
            },
            fail: err => {
                swan.showModal({
                    title: '错误信息',
                    content: JSON.stringify(err)
                });
                updatalist(this.data.displayList,goodidval);
            }
        });
    },
    updatalist(arr,value){
        let newarr=[];
        for(let i=0;i<arr.length;i++){
            if(arr[i].goodId!=value){
                newarr.push(arr[i]);
            }   
        }
        this.setData({
            displayList:newarr
        });
        swan.showModal({
            title:"提示",
            content:"删除成功！"
        });
    }
});